package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Url;
import com.baizhi.springboot.service.UrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UrlController {
    @Autowired
    private UrlService urlService;

    @RequestMapping("/selectUrlByPage")
    public Map selectUrlByPage(Integer page,Integer limit) {
        return urlService.selectUrlByPage(page,limit);
    }
    @RequestMapping("/insertUrl")
    public Map insertUrl(Url url) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            urlService.insertUrl(url);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteUrl")
    public Map  deleteUrl(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            urlService.deleteUrl(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneUrl")
    public Map selectOneUrl(Integer id, HttpSession session) {
        Url url= urlService.selectOneUrl(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url", url);
        return map;
    }

    @RequestMapping("/updateUrl")
    public Map updateUrl(Url url) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //System.out.println(url);
            urlService.updateUrl(url);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
